package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.CgaSupportExecution;
import com.lilosoft.business.precity.dao.UpProSpecialprocedureMapper;
import com.lilosoft.business.precity.models.UpProAccept;
import com.lilosoft.business.precity.models.UpProProcess;
import com.lilosoft.business.precity.models.UpProSpecialprocedure;
import com.lilosoft.business.precity.models.UpProSpecialprocedureExample;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description: 特别程序信息    (暂时没有数据)
 * User: 小峰
 * Date: 2019-08-28
 * Time: 10:42
 */
@Service
public class UpProSpecialprocedureService {
    @Autowired
    private UpProSpecialprocedureMapper upProSpecialprocedureMapper;

    public UpProSpecialprocedure UpProSpecialprocedureSynchronous(UpProProcess upProProcess, UpProAccept upProAccept, CgaSupportExecution cgaExecution){
        UpProSpecialprocedure model = new UpProSpecialprocedure();
        BeanUtils.copyProperties(upProAccept, model);
        model.setRowguid(UUIDUtils.createUUId());
        model.setProjectRowguid(upProAccept.getRowguid());
        model.setProjectno(upProAccept.getProjectno());
        model.setLinkRowguid(upProProcess.getRowguid());
        model.setSpecialtype("2");
        model.setSpecialname(cgaExecution.getSupportName());
        model.setSpecialtime(cgaExecution.getStartTime());
        model.setEndtime(cgaExecution.getEndTime()==null?cgaExecution.getEndTime():new Date());
        if(null != model.getSpecialtime() && model.getSpecialtime().after(model.getEndtime())){
            model.setSpecialtime(model.getEndtime());
            model.setEndtime(cgaExecution.getStartTime());
        }
        model.setSpecialreason(cgaExecution.getReason());
        model.setApplyerusername(cgaExecution.getUserName());
        model.setHandleuser(cgaExecution.getOperaterName());
        model.setResult(cgaExecution.getSupportResult());
        upProSpecialprocedureMapper.insertSelective(model);
        return model;
    }

    /**
     * 根据cdBatch 统计数据个数（特别程序信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch) {
        UpProSpecialprocedureExample example= new UpProSpecialprocedureExample();
        UpProSpecialprocedureExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProSpecialprocedureMapper.countByExample(example);
    }
}
